#include"usingHeader.hpp"

//这个用来从管道内读取数据，将文件作备份

int main(){
    //获取备份得到文件的名字
    string bak_filename = Backup_PATH + file_category;

    //以只读的方式打开管道文件
    int fd_pipe = open(pipe_name.c_str(), O_RDONLY);
    if(fd_pipe < 0){
        ERR_EXIT("open pipe");
    }
    else{
        cout << "成功打开管道文件(O_RDONLY)" << endl;
    }

    //创建备份文件
    umask(0);
    int fd = open(bak_filename.c_str(), O_TRUNC | O_CREAT | O_WRONLY, 0666);
    if(fd < 0){
        ERR_EXIT("open backup_file");
    }
    else{
        cout << "成功打开备份得到的文件" << endl;
    }


    //从管道读取
    int rd = 0;
    char buffer[1024] = {0};
    while((rd = read(fd_pipe, buffer, sizeof(buffer))) > 0){
        int wd = write(fd, buffer, 1024);
        if(wd < 0){
            ERR_EXIT("write to pipe");
        }
    }

    if(rd == 0){
        cout << "内容已经全部写入管道" << endl;
    }
    if(rd < 0){
        ERR_EXIT("read from pipe");
    }
    

    return 0;
}